package com.ruoyi.stuinfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.stuinfo.domain.AcademicInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.Accommodation;
import com.ruoyi.stuinfo.service.IAccommodationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 住宿分配Controller
 *
 * @author zc
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/stuinfo/accommodation")
public class AccommodationController extends BaseController
{
    @Autowired
    private IAccommodationService accommodationService;

    /**
     * 查询住宿分配列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:accommodation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Accommodation accommodation)
    {


        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<Accommodation> list = accommodationService.selectAccommodationList(accommodation);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<Accommodation> list = accommodationService.selectAccommodationList_handteacher(teacherId);
            return getDataTable(list);
        }
        else
        {
            //管理员
            startPage();
            List<Accommodation> list = accommodationService.selectAccommodationList(accommodation);
            return getDataTable(list);
        }

    }

    /**
     * 导出住宿分配列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:accommodation:export')")
    @Log(title = "住宿分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Accommodation accommodation)
    {

        String teacherId=getUsername();
        if("admin".equals(teacherId))
        {
            List<Accommodation> list = accommodationService.selectAccommodationList(accommodation);
            ExcelUtil<Accommodation> util = new ExcelUtil<Accommodation>(Accommodation.class);
            util.exportExcel(response, list, "住宿分配数据");
        }
        else
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<Accommodation> list = accommodationService.selectAccommodationList_handteacher(teacherId);
            ExcelUtil<Accommodation> util = new ExcelUtil<Accommodation>(Accommodation.class);
            util.exportExcel(response, list, "住宿分配数据");
        }

    }

    /**
     * 获取住宿分配详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:accommodation:query')")
    @GetMapping(value = "/{accommodationId}")
    public AjaxResult getInfo(@PathVariable("accommodationId") Long accommodationId)
    {
        return success(accommodationService.selectAccommodationByAccommodationId(accommodationId));
    }

    /**
     * 新增住宿分配
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:accommodation:add')")
    @Log(title = "住宿分配", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Accommodation accommodation)
    {
        return toAjax(accommodationService.insertAccommodation(accommodation));
    }

    /**
     * 修改住宿分配
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:accommodation:edit')")
    @Log(title = "住宿分配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Accommodation accommodation)
    {
        return toAjax(accommodationService.updateAccommodation(accommodation));
    }

    /**
     * 删除住宿分配
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:accommodation:remove')")
    @Log(title = "住宿分配", businessType = BusinessType.DELETE)
	@DeleteMapping("/{accommodationIds}")
    public AjaxResult remove(@PathVariable Long[] accommodationIds)
    {
        return toAjax(accommodationService.deleteAccommodationByAccommodationIds(accommodationIds));
    }
}
